<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller
{
    protected $_templates;
	
    function __construct() 
	{
        parent::__construct();
        $this->pre_message = "";
        $this->load->model('admin_model','admin');
		$this->load->library('permit_library') ;
		      
        $this->permit_library->check_permit();
        $this->session->set_userdata(array('Url'=>uri_string()));
    }
	    
    function listadmin()
	{
        $data['title'] = lang('list_admin');
		$data['add'] = 'admin/add' ;
        $config['base_url'] = base_url().'admin/listadmin/';  
        $config['total_rows']   =  $this->admin->getNumAdmin();
        $data['num'] = $config['total_rows'];
        $config['per_page']  =   20;
        $config['uri_segment'] = 3; 
        $this->pagination->initialize($config);   
        $data['list'] =   $this->admin->getAllAdmin($config['per_page'],$this->uri->segment('3'));
        $data['pagination']    = $this->pagination->create_links();           
        $this->_templates['page'] = 'admin/listadmin';
        $this->site->load('admin/listadmin',$data);
    }
      
    function add()
	{                         
        $data['title'] = lang('add_new_admin');
        $data['list'] = $this->admin->get_list_permit();
        if(!empty($_POST['bt_submit'])){
           $this->form_validation->set_rules('login_name',lang('user_name'),'trim|required|callback__check_admin');  
           $this->form_validation->set_rules('password',lang('password'),'trim|required');  
           $this->form_validation->set_rules('re_password',lang('re_password'),'trim|required|matches[password]'); 
           if($this->form_validation->run() == FALSE){
              $this->pre_message = validation_errors();
           }else{
               if($this->admin->saveAdmin()){
                   $this->session->set_flashdata('message',lang('ntc_save_success'));
                   redirect('admin/listadmin');
               }else{
                   $this->pre_message = lang('err_save_unsuccess');
               }
           } 
        }
        $data['message'] = $this->pre_message;
        $this->_templates['page'] = 'admin/add';
        $this->site->load($this->_templates['page'],$data);
    }
	
    function edit()
	{
        $rs = $this->admin->getAdmin();
        $data['list'] = $this->admin->get_list_permit(); 
        $data['title'] = lang('update_admin_account').' : '.$rs->login_name;
        $data['admin_id'] = $rs->admin_id;
        if(!empty($_POST['bt_submit']))
		{
           $this->form_validation->set_rules('login_name',lang('user_name'),'trim|required|callback__check_admin_edit');  
           $this->form_validation->set_rules('password',lang('password'),'trim');  
           $this->form_validation->set_rules('re_password',lang('re_password'),'trim|matches[password]'); 
           
		   if($this->form_validation->run() == FALSE){
              $this->pre_message = validation_errors();
           }else{ 
               if($this->admin->saveAdmin()){
                   $this->session->set_flashdata('message',lang('ntc_save_success'));
                   redirect('admin/listadmin');
               }else{
                   $this->pre_message = lang('err_save_unsuccess');
               }
           }
           $data['login_name'] = $this->input->post('login_name');
        }else{
            $data['login_name'] = $rs->login_name;
        }
        $data['message'] = $this->pre_message;
        $this->_templates['page'] = 'admin/edit';
        $this->site->load($this->_templates['page'],$data);
    }    
	
	function changepass()
	{
		$data['title'] = 'Đổi mật khẩu' ; 
		if(isset($_POST['bt_submit']))
		{
			$oldpass = $this->input->post('oldpass') ;
			$newpass = $this->input->post('newpass') ;
			$repass = $this->input->post('repass') ;
			
			$admin_id = $this->session->userdata('admin_id');
			$rs = $this->admin->get_id($admin_id) ; 
			$password = $rs->password ;
			
			if(($password == md5($oldpass)) && ($newpass==$repass))
			{
				$req['password'] = md5($newpass);
				$this->db->where('admin_id',$admin_id) ;
				if($this->db->update('sys_admin',$req))
				{
					$this->session->set_flashdata('message','Cập nhật thành công');
					redirect('admincp');
				}else{
					$this->pre_message = 'Lưu không thành công';
				}
			}else{
				$this->pre_message = 'Mật khẩu không đúng hay nhập mật khẩu 2 lần không giống nhau.';
			}
		}
		$data['message'] = $this->pre_message;
		$this->site->load('admin/changepass',$data);
	}
	  
    function _check_admin($login_name)
	{
        $this->db->where('login_name',$login_name);
        $query = $this->db->get('sys_admin');
        if($query->num_rows() > 0){
            $this->form_validation->set_message('_check_admin',lang('login_name_used'));
            return false;
        }else{
            return true;
        }
    }
	
    function _check_admin_edit($login_name)
	{
        $this->db->where('admin_id <>',$this->uri->segment(3));
        $this->db->where('login_name',$login_name);
        $query = $this->db->get('sys_admin');
        if($query->num_rows() > 0){
            $this->form_validation->set_message('_check_admin_edit',lang('login_name_used'));
            return FALSE;
        }else{
            return TRUE;
        }
    }      
      
    function del()
    {
        $id = $this->uri->segment(3);
        $page = $this->uri->segment(4);

        if($this->admin->delete($id))
            $this->session->set_flashdata('message',lang('ntc_del_success'));
        else $this->session->set_flashdata('message',lang('err_del_unsuccess'));

        redirect('admin/listadmin/'.$page);
    }
      
    function dels()
    {
        if(!empty($_POST['ar_id']))
        {
            $page = (int)$this->input->post('page');
            $ar_id = $this->input->post('ar_id');

            if(!empty($_POST['btn_submit']))
            {
                for($i = 0; $i < sizeof($ar_id); $i ++) {
                    if ($ar_id[$i]){
                        if($this->admin->delete($ar_id[$i]))
                            $this->session->set_flashdata('message',lang('ntc_del_success'));
                        else $this->session->set_flashdata('message',lang('err_del_unsuccess'));
                    }
                }
            }
        }
        redirect('admin/listadmin/'.$page);
    }
}
?>
